local InnerMsg = require("core.InnerMsg")
local Util = require("common.Util")
-- 负责管理logic到middle的连接

FD_2_SVRINDEX = FD_2_SVRINDEX or {}
SVRINDEX_2_FD = SVRINDEX_2_FD or {}

FD_2_SVRINDEX_MERGE = FD_2_SVRINDEX_MERGE or {} -- 合服之后一个物理服的serverindex其实可能有多个服
SVRINDEX_2_FD_MERGE = SVRINDEX_2_FD_MERGE or {}

SVRINDEX_2_OPENDAY = SVRINDEX_2_OPENDAY or {}   -- 服务器对应开服天数

-- 根据svrIndex找对应的fd
function getFDBySvrIndex(svrIndex)
    local fd = SVRINDEX_2_FD[svrIndex]
    if fd then
        return fd
    end
    
    return SVRINDEX_2_FD_MERGE[svrIndex]
end

-- 根据初始svrIndex找当前svrIndex
function getLastSvrIndex(srcSvrIndex)
    local fd = getFDBySvrIndex(srcSvrIndex)
    if not fd then
        return
    end
    return FD_2_SVRINDEX[fd]
end


-- 找一个随机连上了跨服的svrIndex
function getRandomSvrIndex(exceptFD)
	local cnt = Util.getTableCount(FD_2_SVRINDEX)
	if cnt == 0 then
		return
	end
	if cnt == 1 then
		for k, v in pairs(FD_2_SVRINDEX) do
			if k ~= exceptFD then
				return v
			end
		end
		return
	end
	
	local tempRet = nil
	local randomIndex = math.random(1, cnt)
	for k, v in pairs(FD_2_SVRINDEX) do
		if k ~= exceptFD then
			tempRet = v
		end
		randomIndex = randomIndex - 1
		if randomIndex <= 0 then
			return tempRet
		end
	end
end

function addLogicServer(fd, svrIndex)
    if fd == nil or svrIndex == nil then 
        assert(nil)
    end
    
    local oldSvrIndex = FD_2_SVRINDEX[fd]
    local oldFd = SVRINDEX_2_FD[svrIndex]
    if oldSvrIndex == svrIndex then return end
    
    FD_2_SVRINDEX[fd] = svrIndex
    SVRINDEX_2_FD[svrIndex] = fd

    if oldSvrIndex and svrIndex ~= oldSvrIndex then
        SVRINDEX_2_FD[oldSvrIndex] = nil
        SVRINDEX_2_OPENDAY[oldSvrIndex] = nil
    end
    
    if oldFd and fd ~= oldFd then
        FD_2_SVRINDEX[oldFd] = nil
    end

    print("addLogicServer", fd, svrIndex)
end

function setServerOpenDay(fd,svrIndex,openTime)
    if fd == nil or svrIndex == nil or openTime == nil then 
        assert(nil)
    end

    SVRINDEX_2_OPENDAY[svrIndex] = openTime
    print("setServerOpenDay",svrIndex,openTime)
end

function delLogicServer(fd)
    if fd == nil then 
        assert(nil)
    end

    local svrIndex = FD_2_SVRINDEX[fd]
    FD_2_SVRINDEX[fd] = nil
    if svrIndex then
         SVRINDEX_2_FD[svrIndex] = nil
    end
    
    local svrMerge = FD_2_SVRINDEX_MERGE[fd]
    FD_2_SVRINDEX_MERGE[fd] = nil
    if svrMerge then
        for i = 1, #svrMerge do
            local tempSvrIndex = svrMerge[i]
            SVRINDEX_2_FD_MERGE[tempSvrIndex] = nil
        end
    end
    
    print("delLogicServer", fd, svrIndex)
end

function onHeartbeat(fd, svrIndexData)
    if #svrIndexData == 0 then
        return
    end
    
    FD_2_SVRINDEX_MERGE[fd] = FD_2_SVRINDEX_MERGE[fd] or {}
    
    
    for i = 1, #svrIndexData do
        local tempSvrIndex = svrIndexData[i]
        FD_2_SVRINDEX_MERGE[fd][i] = tempSvrIndex
        SVRINDEX_2_FD_MERGE[tempSvrIndex] = fd
    end
end

--
function cleanData(fd)
   local svrIndex = FD_2_SVRINDEX[fd]
   if svrIndex == nil then return end


   -- 根据svrIndex 清除各个模块的数据 注意兼容不报错
end

function getOpenDaySvrCnt(openDay,actDay)
    for k,v in pairs(SVRINDEX_2_OPENDAY) do
        if v == 0 then
	    	return 0
	    end
	    
	    local startTime = Util.getDayStartTime(v)
	    
	    local now = os.time()
	    local openTime = math.ceil((now - startTime + 1) / 24 / 3600)
        
        local actOpenDay = openTime - actDay

        if actOpenDay < openDay and actOpenDay >= 0 then
            return 0
        end
    end
    return 1
end

function LW_ABS_ACT_IS_OPEN(fd,msg)
    local isOpen = getOpenDaySvrCnt(msg.openDay,msg.actDay)
    local msgRet = InnerMsg.wl.WL_ABS_ACT_IS_OPEN
    msgRet.funcID = msg.funcID
    msgRet.isOpen = isOpen
    InnerMsg.sendMsg(fd, msgRet)
end